﻿/*
 * ============ [ 废土 ] ============
 *
 * [ 维护者 ]  argoran@gmail.com
 * [  状态  ]  原型
 *
 * ============ [ 注释 ] ============
 *
 * 处理start指令的类
 *
 * ==================================
 */

using System;

using LastLand.Data.Global;
using LastLand.Data.Connection;

using Up2Link.SegString;
using Up2Link.Server;

namespace LastLand.Core.CmdInterpreter
{
    public class CCmdStartServer : ICommandInterpreter
    {
        public void ExecuteCommand(string _cmd, string _args)
        {
            if (_args != null)
            {
                String[] args = _args.Split(CConst.DELIMITER_ARGUMENT);
                if (args.Length >= 2)
                {
                    StartServer(args[0].Trim(), Int32.Parse(args[1].Trim()));
                }
                else
                {
                    StartServer(args[0].Trim(), 8802);
                }
            }
            else
            {
                CData.console.SendOutput("");
                CData.console.SendOutput("你输入的指令是：" + _cmd);
                CData.console.SendOutput("指令参数过少");
            }
        }

        private void StartServer(String _address, Int32 _port)
        {
            if (CData.server == null)
            {
                CData.server = new CServer<CMessageSS>();
                CData.server.Start(_address, _port);

                CData.serverTimerCombatUpdateCounter = CData.serverTimerManager.ActivateTimer(new CMessageSS(CConst.EVENT_COMBAT_UPDATE_COUNTER, 0, 0, ""), new TimeSpan(CTimerManager.MillisecondsToTicks(5000)), true);
                CData.serverTimerWorldUpdateCounter = CData.serverTimerManager.ActivateTimer(new CMessageSS(CConst.EVENT_WORLD_UPDATE_COUNTER, 0, 0, ""), new TimeSpan(CTimerManager.MillisecondsToTicks(10000)), true);
                CData.serverTimerCharacterUpdateCounter = CData.serverTimerManager.ActivateTimer(new CMessageSS(CConst.EVENT_CHARACTER_UPDATE_COUNTER, 0, 0, ""), new TimeSpan(CTimerManager.MillisecondsToTicks(3600000)), true);

                CData.console.SendOutput("");
                CData.console.SendOutput("服务器启动，开始监听 " + _address + ":" + _port);
            }
            else
            {
                CData.console.SendOutput("");
                CData.console.SendOutput("服务器已经启动");
            }
        }
    }
}
